package com.krds
{
	import com.greensock.*;
	import com.greensock.easing.*;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.system.System;
	
	
	
	public class Product extends MovieClip implements IProduct
	{
		var travel:Number;
		var start:Boolean;
		public var clicked:Boolean = false;
		


		public function Product()
		{
			
			this.mouseEnabled = false;		
			travel = Math.round(Math.random()*1);		
			start = (Math.random()*1) < 0.5? false:true;	
			hitMc.addEventListener(MouseEvent.CLICK,proClicked);
			
		}
		
		public function startMove():void
		{
			TweenMax.to(this, 0, {autoAlpha:1});
			var endY:Number;
			var endX:Number;
			if(travel == 0)
			{//y taravel
				
				this.y =- hitMc.y-hitMc.height/2+ Number(start)*(479+hitMc.height);
				this.x = - hitMc.x + hitMc.width/2+ Math.random()*(630-hitMc.width);
				
				endY = - hitMc.y-hitMc.height/2+ Number(!start)*(479+hitMc.height);
				endX =	 - hitMc.x + hitMc.width/2+ Math.random()*(630-hitMc.width);
				
			}else {//x travel
				
				this.y =- hitMc.y + hitMc.height/2+ Math.random()*(479-hitMc.height);
				this.x = - hitMc.x-hitMc.width/2+ Number(start)*(630+hitMc.width);
				
				
				endY = - hitMc.y + hitMc.height/2+ Math.random()*(479-hitMc.height);
				endX = - hitMc.x-hitMc.width/2+ Number(!start)*(630+hitMc.width);
				
			}
			
			
			
			TweenMax.to(this,5,{y:endY,x:endX,onComplete:outOfStage})
						
		}
		
		private function proClicked(e:MouseEvent)
		{
			hitMc.removeEventListener(MouseEvent.CLICK,proClicked);
			stopMove();
			IMain(this.parent.parent).productCliked(this.name)
			clicked = true;		
			
		}
		public function stopMove():void
		{
			TweenMax.to(this,0,{y:this.y,x:this.x})
		}
		public function outOfStage():void
		{
			if(this.parent != null)
			{	
				if(hitMc.hasEventListener(MouseEvent.CLICK))
				{
					hitMc.removeEventListener(MouseEvent.CLICK,proClicked);
				}	
				IMain(this.parent.parent).removeChildren(this);	
				
			}
			
		}
		public function addImage():void
		{
			var bit:BitmapData = new BitmapData(this.width,this.height,true, 0 );	
			bit.draw(this)
			var bitmap:Bitmap = new Bitmap(bit);
			bitmap.smoothing = true;			
			bitmap.x = this.x;
			bitmap.y = this.y;
			IMain(this.parent.parent).addBitmap(bitmap);
			outOfStage()
			
		}
		
	}
}